package com.thti.mecool.client.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.util.Arith;
import com.thti.mecool.client.util.RegexPattern;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.Toolbar;

public class SaleSKUActivity extends BaseActivity {
	private ListView skuList;
	private TextView storeNameTextView;
	private ArrayList<String> ItemNameList;
	private ArrayList<ArrayList<EditText>> EditList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		noTitle();
		setContentView(R.layout.mecool_single_list);
		initToolbar();
		initData();
		storeNameTextView=(TextView)findViewById(R.id.mecool_single_list_storename);
		storeNameTextView.setText(getIntent().getStringExtra("titlename"));
		skuList = (ListView) findViewById(R.id.mecool_single_list_listview);
		skuList.setAdapter(skuAdapter);
	}

	@SuppressWarnings("unchecked")
	private void initData() {
		ItemNameList = (ArrayList<String>) getIntent().getSerializableExtra(
				"ItemNameList");
		EditList=new ArrayList<ArrayList<EditText>>();
		Button save=(Button)findViewById(R.id.mecool_single_list_save);
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(getIntent().getBooleanExtra("isadd", false)){
					Intent intent=new Intent(SaleSKUActivity.this,SaleVolumeActivity.class);
					intent.putExtra("name", getIntent().getStringExtra("titlename"));
					intent.putExtra("isadd", getIntent().getBooleanExtra("isadd", false));
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "/" + "MM" + "/" + "dd"
							 + " HH:mm");
					Date curDate = new Date(System.currentTimeMillis());
					intent.putExtra("time", sdf.format(curDate));
					startActivity(intent);
					finish();
				}else{
					Save(true,-1);
					Util.sendToast(SaleSKUActivity.this, "结果已保存！");
					finish();
				}
			}
		});
	}
	
	private BaseAdapter skuAdapter = new BaseAdapter() {
		Integer index=-1;
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ViewHolder holder=null;
			if (arg1 == null) {
				holder=new ViewHolder();
				arg1 = getLayoutInflater().inflate(R.layout.mecool_sale_sku_item, null);
				holder.id = (TextView) arg1.findViewById(R.id.mecool_sale_sku_id);
				holder.item = (TextView) arg1
						.findViewById(R.id.mecool_sale_sku_item);
				holder.quantity = (EditText) arg1
						.findViewById(R.id.mecool_sale_sku_quantity);
				holder.univalent = (EditText) arg1
						.findViewById(R.id.mecool_sale_sku_univalent);
				holder.total = (EditText) arg1.findViewById(R.id.mecool_sale_sku_total);
				holder.quantity.addTextChangedListener(textWatcher);
				holder.univalent.addTextChangedListener(textWatcher);
				ArrayList<EditText> list=new ArrayList<EditText>();
				list.add(holder.quantity);
				list.add(holder.univalent);
				list.add(holder.total);
				EditList.add(list);
				

				holder.quantity.setTag(arg0);
				holder.univalent.setTag(arg0);
				holder.total.setTag(arg0);
				
				holder.quantity.setOnTouchListener(onTouchListener);
				holder.univalent.setOnTouchListener(onTouchListener);
				holder.total.setOnTouchListener(onTouchListener);
				
				holder.id.setText(arg0 + 1 + "");
				holder.item.setText(ItemNameList.get(arg0));
			}
			
			return arg1;
		}

		OnTouchListener onTouchListener=new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					index = (Integer) v.getTag();
				}
				return false;
			}
		};
		
		
		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return ItemNameList.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return ItemNameList.size();
		}
		class ViewHolder {
			TextView id, item;
			EditText quantity,univalent,total;
		}
		
		
		private TextWatcher textWatcher = new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}
			@Override
			public void afterTextChanged(Editable arg0) {
				if(!RegexPattern.isNumber(arg0.toString())&&arg0.length()>0){
					for(EditText et:EditList.get(index)){
						if(et.getText().toString().equals(arg0.toString())){
							et.setError("只能输入数字！");
						}
					}
				}
				Save(false,index);
			}
		};
	};
	
	private void Save(boolean isSave,int index){
		if(isSave){
		for(ArrayList<EditText> views:EditList){
			setTotal(views);
			//存结果
		}
		}else{
		ArrayList<EditText> views=EditList.get(index);
		setTotal(views);
		}
	}
	private void setTotal(ArrayList<EditText> views){
		String a = null,b = null,result=null;
		a=views.get(0).getText().toString();
		b=views.get(1).getText().toString();
		try{
			result=Arith.mul(a,b);
			views.get(2).setText(result);
		}catch(NumberFormatException nfe){
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText(getIntent().getStringExtra("uploadtitle"));
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
	}};
	
}
